<!DOCTYPE html>
<html>
<!--
Copyright 2011 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.fx.Animation</title>
<script type="text/javascript" src="../base.js"></script>
<script type="text/javascript">
  goog.require('goog.events');
  goog.require('goog.fx.Animation');
  goog.require('goog.fx.AnimationParallelQueue');
  goog.require('goog.fx.AnimationQueue');
  goog.require('goog.fx.AnimationSerialQueue');
  goog.require('goog.testing.MockClock');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script type="text/javascript">
  goog.fx.anim.setAnimationWindow(null);

  var clock;

  function setUpPage() {
    clock = new goog.testing.MockClock(true);
  }

  function tearDownPage() {
    clock.dispose();
  }

  function testParallelEvents() {
    var anim = new goog.fx.AnimationParallelQueue();
    anim.add(new goog.fx.Animation([0], [100], 200));
    anim.add(new goog.fx.Animation([0], [100], 400));
    anim.add(new goog.fx.Animation([0], [100], 600));

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isStopped());
    assertTrue(anim.queue[2].isStopped());
    assertTrue(anim.isStopped());

    var playEvents = 0, beginEvents = 0, resumeEvents = 0, pauseEvents = 0;
    var endEvents = 0, stopEvents = 0, finishEvents = 0;

    goog.events.listen(anim, goog.fx.Transition.EventType.PLAY, function() {
      ++playEvents; });
    goog.events.listen(anim, goog.fx.Transition.EventType.BEGIN, function() {
      ++beginEvents; });
    goog.events.listen(anim, goog.fx.Transition.EventType.RESUME, function() {
      ++resumeEvents; });
    goog.events.listen(anim, goog.fx.Transition.EventType.PAUSE, function() {
      ++pauseEvents; });
    goog.events.listen(anim, goog.fx.Transition.EventType.END, function() {
      ++endEvents; });
    goog.events.listen(anim, goog.fx.Transition.EventType.STOP, function() {
      ++stopEvents; });
    goog.events.listen(anim, goog.fx.Transition.EventType.FINISH, function() {
      ++finishEvents; });

    // PLAY, BEGIN
    anim.play();
    // No queue events.
    clock.tick(100);
    // PAUSE
    anim.pause();
    // No queue events
    clock.tick(200);
    // PLAY, RESUME
    anim.play();
    // No queue events.
    clock.tick(400);
    // END, STOP
    anim.stop();
    // PLAY, BEGIN
    anim.play();
    // No queue events.
    clock.tick(400);
    // END, FINISH
    clock.tick(200);

    // Make sure the event counts are right.
    assertEquals(3, playEvents);
    assertEquals(2, beginEvents);
    assertEquals(1, resumeEvents);
    assertEquals(1, pauseEvents);
    assertEquals(2, endEvents);
    assertEquals(1, stopEvents);
    assertEquals(1, finishEvents);
  }

  function testSerialEvents() {
    var anim = new goog.fx.AnimationSerialQueue();
    anim.add(new goog.fx.Animation([0], [100], 100));
    anim.add(new goog.fx.Animation([0], [100], 200));
    anim.add(new goog.fx.Animation([0], [100], 300));

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isStopped());
    assertTrue(anim.queue[2].isStopped());
    assertTrue(anim.isStopped());

    var playEvents = 0, beginEvents = 0, resumeEvents = 0, pauseEvents = 0;
    var endEvents = 0, stopEvents = 0, finishEvents = 0;

    goog.events.listen(anim, goog.fx.Transition.EventType.PLAY, function() {
      ++playEvents; });
    goog.events.listen(anim, goog.fx.Transition.EventType.BEGIN, function() {
      ++beginEvents; });
    goog.events.listen(anim, goog.fx.Transition.EventType.RESUME, function() {
      ++resumeEvents; });
    goog.events.listen(anim, goog.fx.Transition.EventType.PAUSE, function() {
      ++pauseEvents; });
    goog.events.listen(anim, goog.fx.Transition.EventType.END, function() {
      ++endEvents; });
    goog.events.listen(anim, goog.fx.Transition.EventType.STOP, function() {
      ++stopEvents; });
    goog.events.listen(anim, goog.fx.Transition.EventType.FINISH, function() {
      ++finishEvents; });

    // PLAY, BEGIN
    anim.play();
    // No queue events.
    clock.tick(100);
    // PAUSE
    anim.pause();
    // No queue events
    clock.tick(200);
    // PLAY, RESUME
    anim.play();
    // No queue events.
    clock.tick(400);
    // END, STOP
    anim.stop();
    // PLAY, BEGIN
    anim.play(true);
    // No queue events.
    clock.tick(400);
    // END, FINISH
    clock.tick(200);

    // Make sure the event counts are right.
    assertEquals(3, playEvents);
    assertEquals(2, beginEvents);
    assertEquals(1, resumeEvents);
    assertEquals(1, pauseEvents);
    assertEquals(2, endEvents);
    assertEquals(1, stopEvents);
    assertEquals(1, finishEvents);
  }

  function testParallelPause() {
    var anim = new goog.fx.AnimationParallelQueue();
    anim.add(new goog.fx.Animation([0], [100], 100));
    anim.add(new goog.fx.Animation([0], [100], 200));
    anim.add(new goog.fx.Animation([0], [100], 300));

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isStopped());
    assertTrue(anim.queue[2].isStopped());
    assertTrue(anim.isStopped());

    anim.play();

    assertTrue(anim.queue[0].isPlaying());
    assertTrue(anim.queue[1].isPlaying());
    assertTrue(anim.queue[2].isPlaying());
    assertTrue(anim.isPlaying());

    clock.tick(100);

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isPlaying());
    assertTrue(anim.queue[2].isPlaying());
    assertTrue(anim.isPlaying());

    anim.pause();

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isPaused());
    assertTrue(anim.queue[2].isPaused());
    assertTrue(anim.isPaused());

    clock.tick(200);

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isPaused());
    assertTrue(anim.queue[2].isPaused());
    assertTrue(anim.isPaused());

    anim.play();

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isPlaying());
    assertTrue(anim.queue[2].isPlaying());
    assertTrue(anim.isPlaying());

    clock.tick(100);

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isStopped());
    assertTrue(anim.queue[2].isPlaying());
    assertTrue(anim.isPlaying());

    anim.pause();

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isStopped());
    assertTrue(anim.queue[2].isPaused());
    assertTrue(anim.isPaused());

    clock.tick(200);

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isStopped());
    assertTrue(anim.queue[2].isPaused());
    assertTrue(anim.isPaused());

    anim.play();

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isStopped());
    assertTrue(anim.queue[2].isPlaying());
    assertTrue(anim.isPlaying());

    clock.tick(100);

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isStopped());
    assertTrue(anim.queue[2].isStopped());
    assertTrue(anim.isStopped());
  }

  function testSerialPause() {
    var anim = new goog.fx.AnimationSerialQueue();
    anim.add(new goog.fx.Animation([0], [100], 100));
    anim.add(new goog.fx.Animation([0], [100], 200));
    anim.add(new goog.fx.Animation([0], [100], 300));

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isStopped());
    assertTrue(anim.queue[2].isStopped());
    assertTrue(anim.isStopped());

    anim.play();

    assertTrue(anim.queue[0].isPlaying());
    assertTrue(anim.queue[1].isStopped());
    assertTrue(anim.queue[2].isStopped());
    assertTrue(anim.isPlaying());

    clock.tick(100);

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isPlaying());
    assertTrue(anim.queue[2].isStopped());
    assertTrue(anim.isPlaying());

    anim.pause();

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isPaused());
    assertTrue(anim.queue[2].isStopped());
    assertTrue(anim.isPaused());

    clock.tick(400);

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isPaused());
    assertTrue(anim.queue[2].isStopped());
    assertTrue(anim.isPaused());

    anim.play();

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isPlaying());
    assertTrue(anim.queue[2].isStopped());
    assertTrue(anim.isPlaying());

    clock.tick(200);

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isStopped());
    assertTrue(anim.queue[2].isPlaying());
    assertTrue(anim.isPlaying());

    anim.pause();

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isStopped());
    assertTrue(anim.queue[2].isPaused());
    assertTrue(anim.isPaused());

    clock.tick(300);

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isStopped());
    assertTrue(anim.queue[2].isPaused());
    assertTrue(anim.isPaused());

    anim.play();

    clock.tick(300);

    assertTrue(anim.queue[0].isStopped());
    assertTrue(anim.queue[1].isStopped());
    assertTrue(anim.queue[2].isStopped());
    assertTrue(anim.isStopped());
  }
</script>
</body>
